<?php
namespace Admin\Controller;
use Think\Controller;
/**
 * 后台管理员对管理员的管理
 * 以及权限的管理
 */


class AdminController extends CommonController {

		public function index()
		{

			//管理员列表的的分页
			 $count =  M('user')->where(array('is_admin'=>'1','shw'=>'1'))->count();
			 $this->assign('count',$count);
			$Page = new \Think\Page($count,5);
			$show = $Page->show();
			
			$this->assign('page',$show);
		
			$sql = "select t1.*,t3.title from user t1 inner join auth_group_access t2 on t1.id=t2.uid inner join auth_group t3 on t2.group_id=t3.id where is_admin='1' and shw='1' order by create_time desc limit ".$Page->firstRow.",".$Page->listRows;
			
			$user = M('user')->query($sql);
			// var_dump($user);
			$this->assign('users',$user);
			// var_dump(md5(123));
			$this->display();
		}

		//后台用户的状态
		public function memberstop()
		{
				$uid = I('post.uid');
				$res =M('user')->where(array('id'=>$uid))->getField('stop');
	    	if($res==0){
	    		M('user')->where(array('id'=>$uid))->save(array('stop'=>1));
	    		$arr = array(	
	    			'error'=>0,
	    			'info'=>'已停用'
	    			);
	    		echo json_encode($arr);
	    		exit;

	    	}else{
	    		M('user')->where(array('id'=>$uid))->save(array('stop'=>0));
	    		$arr = array(
	    			'error'=>1,
	    			'info'=>'启用'
	    			);
	    		echo json_encode($arr);
	    		exit;
	    	}
		}
		//添加用户展示
		public function add_data()
		{
			$user = M('user')->field('id,nickname')->where(array('is_admin'=>'1','shw'=>'1'))->select();
			// var_dump($user);

			$role = M('auth_group')->field('id,title')->select();
			
			// var_dump($role);
			$this->assign('role',$role);

			$this->assign('user',$user);
			$this->display();
		}

		//添加管理员
		public function checkdate()
		{

			// var_dump(I('post.'));
			$data['uid'] = I('post.user');
			$data['group_id'] = I('post.role');
			if (M('auth_group_access')->add($data)) {
				$arr = array(
						'error'=>0,
						'info'=>'添加成功'
							);
				echo json_encode($arr);
			}else{
				$arr = array(
						'error'=>1,
						'info'=>'添加失败'
							);
				echo json_encode($arr);
			}
			

		}


				//基本设置展示
		public function edit()
		{
			$id = I('get.id');

			// var_dump($id);

			$res = M('user')->join('auth_group_access t1 on user.id=t1.uid')->join('auth_group t2 on t2.id=t1.group_id')->where(array('user.id'=>$id))->field('user.*,t2.title')->find();

			// var_dump($res);
			$role = M('auth_group')->field('title,id')->select();
			// var_dump($role);
			$this->assign('role',$role);
			$this->assign('res',$res);
			// layout('left');
			$this->display();
			
			
		}



					//更新基本设置数据数据,用户里面的修改
			public function editdate()
			{
				$data = I('post.');
				
				
				$user['nickname']=$data['username'];
				$user['email']=$data['email'];
				// $data['role']=$data['title'];
				// $user['password']=md5($data['pass']);
				$user['id'] = $data['id'];
				$id = $user['id'];
				
				// exit();
				$sql = "select * from user where id!={$id} && (email='{$user['email']}' || nickname='{$user['nickname']}')";
				$res = M('user')->query($sql);
				
				if ($res) {
					$arr = array(
					'error'=>1,
					'info'=>'邮箱或者用户名已经存在'
				);
				echo json_encode($arr);
				}else{
						// $role['uid']=$id;
						$role['group_id']=$data['role'];
						// var_dump($role);
						// var_dump($user);

						$uinfo =M('user')->save($user);
						$ainfo = M('auth_group_access')->where(array('uid'=>$id))->save($role);
						if($uinfo||$ainfo){
							$arr = array(
									'error'=>0,
									'info'=>'修改成功'
								);
							echo json_encode($arr);
						}else{
								$arr = array(
									'error'=>2,
									'info'=>'修改失败'
								);
							echo json_encode($arr);
						}
				}
				


			}



			// //管理员修改管理员密码界面
			// public function member_password()
			// {
			// 	$id = I('get.id');
			// 	$user = M('user')->find($id);
			// 	$this->assign('user',$user);
			// 	$this->display();
			// }
			

						//批量删除用户(定个状态)
	   	public function delAll()
	   	{
	   		$id = current(I('post.'));
	   		
	   		$id = rtrim($id,',');
	   		$arr = explode(',', $id);
	   		// var_dump($arr);exit();
	   		// $sql = "update question set show='1' where id in ({$id})";
	   		foreach ($arr as $id) {
	   			$sql = "update user set shw='1' where id='{$id}'";
				M()->execute($sql);
	   		}
	   		// var_dump($sql);
	   				   	$arr = array(
		   				'error'=>0,
		   				'info'=>"删除成功",
		   				// 'url'=>U('admin/question/index')
		   			);
		   		 echo json_encode($arr);
		   		 exit();
	   		
	   	}
	   	   

			//批量恢复用户(定个状态)
	   	public function recoverAll()
	   	{
	   		$id = current(I('post.'));
	   		
	   		$id = rtrim($id,',');
	   		$arr = explode(',', $id);
	   		// var_dump($arr);exit();
	   		// $sql = "update question set show='1' where id in ({$id})";
	   		foreach ($arr as $id) {
	   			$sql = "update user set shw='1' where id='{$id}'";
				M()->execute($sql);
	   		}
	   		// var_dump($sql);
	   				   	$arr = array(
		   				'error'=>0,
		   				'info'=>"恢复成功",
		   				// 'url'=>U('admin/question/index')
		   			);
		   		 echo json_encode($arr);
		   		 exit();
	   		
	   	}
	   	   	//删除一条用户(状态)
	   public function	delte()
	   {
	   	  $id=current(I('post.'));
	   	  // $sql = "delete from question where id={$id}";
	   	 $sql = "update user set shw='0' where id={$id}";

	   	  if ( M()->execute($sql)) {
		   	 
		   	  $arr = array(
		   				'error'=>0,
		   				'info'=>"删除成功"
		   			);
		   	  echo json_encode($arr);
	   	  }
	   	 
	   }

	   //删除用户的展示
	   		public function del()
		{

			//用户列表的的分页
			 $count =  M('user')->where(array('is_admin'=>'1','shw'=>'0'))->count();
			 $this->assign('count',$count);
			$Page = new \Think\Page($count,5);
			$show = $Page->show();
			
			$this->assign('page',$show);
		
			$sql = "select t1.*,t3.title from user t1 inner join auth_group_access t2 on t1.id=t2.uid inner join auth_group t3 on t2.group_id=t3.id where is_admin='1' and shw='0' order by create_time desc limit ".$Page->firstRow.",".$Page->listRows;
			
			$user = M('user')->query($sql);
			// var_dump($user);
			$this->assign('users',$user);
			$this->display();
		}

		  public function recover()
	   {
	   		 $id=current(I('post.'));
	   		 $sql = "update user set shw='1' where id={$id}";
			 if ( M()->execute($sql)) {
		   	 
		   	  $arr = array(
		   				'error'=>0,
		   				'info'=>"恢复成功"
		   			);
		   	  echo json_encode($arr);
	   	  }
	   }
	      	//彻底删除一条问题
	   public function	delteone()
	   {
	   	  $id=current(I('post.'));
	   	  $sql = "delete from user where id={$id}";
	   	 // $sql = "update question set shw='1' where id={$id}";

	   	  if ( M()->execute($sql)) {
		   	 
		   	  $arr = array(
		   				'error'=>0,
		   				'info'=>"删除成功"
		   			);
		   	  echo json_encode($arr);
	   	  }
	   	 
	   }
	 


}


?>